package com.aj.controller;


import com.aj.Interface.MemberService;
import com.aj.Interface.ReportService;
import com.aj.Interface.SetmealService;
import com.aj.common.MessageConstant;
import com.aj.pojo.CheckItem;
import com.aj.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.Result;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;

    @Reference
    private SetmealService setmealService;

    @Reference
    private ReportService reportService;

    @RequestMapping("/exportBusinessReport")
    public Result  exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
     try{
        Map<String, Object> result = reportService.getBusinessReport();
        String reportDate = (String) result.get("reportDate");
        Integer todayNewMember = (Integer) result.get("todayNewMember");
        Integer totalMember = (Integer) result.get("totalMember");
        Integer thisWeekNewMember = (Integer) result.get("thisWeekNewMember");
        Integer thisMonthNewMember = (Integer) result.get("thisMonthNewMember");
        Integer todayOrderNumber = (Integer) result.get("todayOrderNumber");
        Integer todayVisitsNumber = (Integer) result.get("todayVisitsNumber");
        Integer thisWeekOrderNumber = (Integer) result.get("thisWeekOrderNumber");
        Integer thisWeekVisitsNumber = (Integer) result.get("thisWeekVisitsNumber");
        Integer thisMonthOrderNumber = (Integer) result.get("thisMonthOrderNumber");
        Integer thisMonthVisitsNumber = (Integer) result.get("thisMonthVisitsNumber");
        List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");

        String temlateRealPath =  request.getSession().getServletContext().getRealPath("template") + File.separator + "report_template.xlsx";

        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
        XSSFSheet sheet = workbook.getSheetAt(0);

        XSSFRow row = sheet.getRow(2);
        row.getCell(5).setCellValue(reportDate);

        sheet.getRow(4).getCell(5).setCellValue(todayNewMember);

        sheet.getRow(4).getCell(7).setCellValue(totalMember);

        sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);

        sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);

        sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);

        sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);

        sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);

        sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);

        sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);

        sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);

        int rowNum=12;
        for (Map map : hotSetmeal) {
            String name = (String) map.get("name");
            Long setmeal_count = (Long) map.get("setmeal_count");
            BigDecimal proportion = (BigDecimal) map.get("proportion");
             row = sheet.getRow(rowNum++);
             row.getCell(4).setCellValue(name);
            row.getCell(5).setCellValue(setmeal_count);
            row.getCell(6).setCellValue(proportion.doubleValue());

        }

        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=report.xlsx");
         workbook.write(outputStream);

        return null;
    }catch (Exception e){
        return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL,null);
    }
    }


    @RequestMapping("/getMemberGoupReport")
    public Result getMemberGoupReport(){
        System.out.println(111);
        Map memberageReport=null;
        try {
            memberageReport =  memberService.getMemberAgeReport();
            List<Map<String,Object>> sexList = memberService.getMemberSexReport();

            memberageReport.put("sexList",sexList);



        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_SEX_REPORT_FAIL);
        }
        return new Result(true, MessageConstant.GET_MEMBER_SEX_REPORT_SUCCESS,memberageReport);
    }

    @RequestMapping("/getBusinessReportData")
    public Result  getBusinessReportData() {
        try {

            Map<String, Object> result =   reportService.getBusinessReport();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result  getSetmealReport() {

     List<Map<String,Object>> setmealCount =  setmealService.findSetmealCount();
     Map map = new HashMap();
     map.put("setmealCount", setmealCount);
     List<String> setmealNames =new ArrayList<>();
        for (Map<String, Object> stringObjectMap : setmealCount) {
            String name = (String) stringObjectMap.get("name");
            setmealNames.add(name);
        }
        map.put("setmealNames",setmealNames);

            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
    }

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {

        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.MONTH,-12 );
        ArrayList<String> list = new ArrayList();
        for (int i = 0; i < 12; i++) {
            calendar.add(calendar.MONTH,1 );
            list.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        HashMap<String,Object> map = new HashMap();
        map.put("months", list);

        List<Integer> memberCount = memberService.findMemberCountByMonth(list);
        map.put("memberCount",memberCount);

        return  new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

    }
    @RequestMapping("/getMemberReportByDate")
    public Result getMemberReportByDate(@RequestBody Map map) throws Exception {
        Map<String,Object> result=new HashMap<>();
      try{  System.out.println(map);
        String startDate= (String) map.get("startDate");
        String endDate= (String) map.get("endDate");
        Date StartDate=DateUtils.parseString2Date(startDate);
        Date EndDate= DateUtils.parseString2Date(endDate);
        startDate=startDate.substring(0,7);
        endDate=endDate.substring(0,7);
        List<String> monthBetween = DateUtils.getMonthBetween(startDate, endDate, "yyyy-MM");
        List<Integer> memberCount=reportService.getMemberCountByDate(monthBetween);
        result.put("months",monthBetween);
        result.put("counts",memberCount);
      return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,result);
      }catch (Exception e){
        return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);}
    }
}
